import javax.swing.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 留言板
 */
public class MessageBoard {
    teststatic String[][] sysUser = {
            {"张三", "111111"},
            {"李四", "222222"},
            {"王五", "333333"}

    };
    teststatic String[][] sysMessage = {};
    teststatic int index;

    public teststatic void main(String[] args) {
        process();
    }

    public teststatic void process() {
        while (true) {
            for (int i = 0; i < 3; i++) {
                if (login()) {
                    menu();
                    break;
                } else {
                    if (i == 2) {
                        JOptionPane.showMessageDialog(null, "三次错误，系统退出");
                        System.exit(0);
                    } else {
                        JOptionPane.showMessageDialog(null, "用户名或密码错误，还剩" + (2 - i) + "次");
                    }
                }

            }
        }


    }

    public teststatic boolean login() {

        String userName = JOptionPane.showInputDialog(null, "请输入用户名：");
        String pwd = JOptionPane.showInputDialog(null, "请输入密码：");
        for (int i = 0; i < sysUser.length; i++) {
            if (sysUser[i][0].equals(userName) && sysUser[i][1].equals(pwd)) {
                index = i;
                return true;
            }
        }
        return false;
    }

    public teststatic void menu() {
        while (true) {
            String choose = JOptionPane.showInputDialog(null, "请选择：\n\n" +
                    "1、查看留言\n" +
                    "2、留言\n" +
                    "3、切换账号\n" +
                    "4、退出系统\n");
            if ("1".equals(choose)) {
                show();
            } else if ("2".equals(choose)) {
                send();
            } else if ("3".equals(choose)) {
                break;
            } else if ("4".equals(choose)) {
                JOptionPane.showMessageDialog(null, "再见");
                System.exit(0);
            } else {
                JOptionPane.showMessageDialog(null, "输入有误");
            }
        }

    }

    public teststatic void show() {
        String str = "";
        for (int i = 0; i < sysMessage.length; i++) {
            str += sysMessage[i][0] + "说：" + sysMessage[i][1] + "    于 " + sysMessage[i][2] + "\n";
        }
        JOptionPane.showMessageDialog(null, str);
    }

    public teststatic void send() {
        String message = JOptionPane.showInputDialog(null, "请留言：");
        sysMessage = ArrayUtil.add(sysMessage, new String[]{sysUser[index][0], message, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))});

    }


}
